home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DKBSRC.ARJ / DUMP.C < prev    next >
C/C++ Source or Header  |  1991-05-07  |  10KB  |  363 lines

  1. /*****************************************************************************
  2. *
  3. *                                     dump.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module contains the code to read and write the default file format
  8. *  generated by DKBTrace.  The format is as follows:
  9. *
  10. *
  11. *  (header:)
  12. *    wwww hhhh       - Width, Height (16 bits, LSB first)
  13. *
  14. *  (each scanline:)
  15. *    llll            - Line number (16 bits, LSB first)
  16. *    rr rr rr ...    - Red data for line (8 bits per pixel,
  17. *                       left to right, 0-255 (255=bright, 0=dark))
  18. *    gg gg gg ...    - Green data for line (8 bits per pixel,
  19. *                       left to right, 0-255 (255=bright, 0=dark))
  20. *    bb bb bb ...    - Blue data for line (8 bits per pixel,
  21. *                       left to right, 0-255 (255=bright, 0=dark))
  22. *
  23. * This software is freely distributable. The source and/or object code may be
  24. * copied or uploaded to communications services so long as this notice remains
  25. * at the top of each file.  If any changes are made to the program, you must
  26. * clearly indicate in the documentation and in the programs startup message
  27. * who it was who made the changes. The documentation should also describe what
  28. * those changes were. This software may not be included in whole or in
  29. * part into any commercial package without the express written consent of the
  30. * author.  It may, however, be included in other public domain or freely
  31. * distributed software so long as the proper credit for the software is given.
  32. *
  33. * This software is provided as is without any guarantees or warranty. Although
  34. * the author has attempted to find and correct any bugs in the software, he
  35. * is not responsible for any damage caused by the use of the software.  The
  36. * author is under no obligation to provide service, corrections, or upgrades
  37. * to this package.
  38. *
  39. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  40. * about them.  Also, if you have any comments or questions, you may contact me
  41. * at the following address:
  42. *
  43. *     David Buck
  44. *     22C Sonnet Cres.
  45. *     Nepean Ontario
  46. *     Canada, K2H 8W7
  47. *
  48. *  I can also be reached on the following bulleton boards:
  49. *
  50. *     OMX              (613) 731-3419
  51. *     Mystic           (613) 596-4249  or  (613) 596-4772
  52. *
  53. *  Fidonet:   1:163/109.9
  54. *  Internet:  dbuck@ccs.carleton.ca
  55. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  56. *
  57. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  58. *
  59. *     The "You Can Call Me RAY" BBS (708) 358-5611
  60. *     The Information Exchange BBS  (708) 945-5575
  61. *
  62. *****************************************************************************/
  63.  
  64. #include "frame.h"
  65. #include "dkbproto.h"
  66.  
  67. FILE_HANDLE *Get_Dump_File_Handle()
  68.    {
  69.    FILE_HANDLE *handle;
  70.  
  71.    if ((handle = (FILE_HANDLE *) malloc(sizeof(FILE_HANDLE))) == NULL) {
  72.       fprintf (stderr, "Cannot allocate memory for output file handle\n");
  73.       return(NULL);
  74.       }
  75.  
  76.    handle->Default_File_Name_p = Default_Dump_File_Name;
  77.    handle->Open_File_p = Open_Dump_File;
  78.    handle->Write_Line_p = Write_Dump_Line;
  79.    handle->Read_Line_p = Read_Dump_Line;
  80.    handle->Read_Image_p = Read_Dump_Image;
  81.    handle->Close_File_p = Close_Dump_File;
  82.    return (handle);
  83.    }
  84.  
  85. char *Default_Dump_File_Name()
  86.    {
  87.    return ("data.dis");
  88.    }
  89.  
  90. int Open_Dump_File (handle, name, width, height, buffer_size, mode)
  91.    FILE_HANDLE *handle;
  92.    char *name;
  93.    int *width;
  94.    int *height;
  95.    int buffer_size;
  96.    int mode;
  97.    {
  98.    int data1, data2;
  99.  
  100.    handle->mode = mode;
  101.    handle->filename = name;
  102.  
  103.    switch (mode) {
  104.       case READ_MODE:
  105.          if ((handle->file = fopen (name, "rb")) == NULL) {
  106.             return(0);
  107.             }
  108.  
  109.          if (buffer_size != 0) {
  110.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  111.                return(0);
  112.  
  113.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  114.             }
  115.  
  116.          if (((data1 = getc(handle->file)) == EOF)
  117.              || ((data2 = getc(handle->file)) == EOF))
  118.             return(0);
  119.  
  120.          *width  = data2 * 256 + data1;
  121.  
  122.          if (((data1 = getc(handle->file)) == EOF)
  123.              || ((data2 = getc(handle->file)) == EOF))
  124.             return(0);
  125.  
  126.          *height = data2 * 256 + data1;
  127.          handle->width = *width;
  128.          handle->height = *height;
  129.          handle->buffer_size = buffer_size;
  130.          break;
  131.  
  132.       case WRITE_MODE:
  133.          if ((handle->file = fopen (name, "wb")) == NULL)
  134.             return(0);
  135.  
  136.          if (buffer_size != 0) {
  137.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  138.                return(0);
  139.  
  140.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  141.             }
  142.  
  143.          putc(*width % 256, handle->file);  /* write to either type of file */
  144.          putc(*width / 256, handle->file);
  145.          putc(*height % 256, handle->file);
  146.          putc(*height / 256, handle->file);
  147.  
  148.          handle->width = *width;
  149.          handle->height = *height;
  150.          handle->buffer_size = buffer_size;
  151.  
  152.          break;
  153.  
  154.       case APPEND_MODE:
  155.          if ((handle->file = fopen (name, "ab")) == NULL)
  156.             return(0);
  157.  
  158.          if (buffer_size != 0) {
  159.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  160.                return(0);
  161.  
  162.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  163.             }
  164.  
  165.          handle->buffer_size = buffer_size;
  166.          break;
  167.       }
  168.    return(1);
  169.    }
  170.  
  171. void Write_Dump_Line (handle, line_data, line_number)
  172.    FILE_HANDLE *handle;
  173.    COLOUR *line_data;
  174.    int line_number;
  175.    {
  176.    register int x;
  177.  
  178.    putc(line_number % 256, handle->file);
  179.    putc(line_number / 256, handle->file);
  180.  
  181.    for (x = 0 ; x < handle->width ; x++)
  182.       putc((int) floor (line_data[x].Red * 255.0), handle->file);
  183.  
  184.    for (x = 0 ; x < handle->width ; x++)
  185.       putc((int) floor (line_data[x].Green * 255.0), handle->file);
  186.  
  187.    for (x = 0 ; x < handle->width ; x++)
  188.       putc((int) floor (line_data[x].Blue * 255.0), handle->file);
  189.  
  190.    if (handle->buffer_size == 0) {
  191.       fflush(handle->file);                       /* close and reopen file for */
  192.       handle->file = freopen(handle->filename, "ab",
  193.                     handle->file);                /* integrity in case we crash*/
  194.       }
  195.    }
  196.  
  197. int Read_Dump_Line (handle, line_data, line_number)
  198.    FILE_HANDLE *handle;
  199.    COLOUR *line_data;
  200.    int *line_number;
  201.    {
  202.    int data, i, c;
  203.  
  204.    if ((c = getc(handle->file)) == EOF) {
  205.       return (0);
  206.       }
  207.  
  208.    *line_number = c;
  209.  
  210.    if ((c = getc(handle->file)) == EOF)
  211.       return (-1);
  212.  
  213.    *line_number += c*256;
  214.  
  215.    for (i = 0 ; i < handle->width ; i++) {
  216.       if ((data = getc(handle->file)) == EOF)
  217.          return(-1);
  218.  
  219.       line_data[i].Red = (DBL) data / 255.0;
  220.       }
  221.  
  222.    for (i = 0 ; i < handle->width ; i++) {
  223.       if ((data = getc(handle->file)) == EOF)
  224.          return(-1);
  225.  
  226.       line_data[i].Green = (DBL) data / 255.0;
  227.       }
  228.  
  229.    for (i = 0 ; i < handle->width ; i++) {
  230.       if ((data = getc(handle->file)) == EOF)
  231.          return(-1);
  232.  
  233.       line_data[i].Blue = (DBL) data / 255.0;
  234.       }
  235.  
  236.    return (1);
  237.    }
  238.  
  239. int Read_Dump_Int_Line (handle, line_data, line_number)
  240.    FILE_HANDLE *handle;
  241.    IMAGE_LINE *line_data;
  242.    int *line_number;
  243.    {
  244.    int data, i, c;
  245.  
  246.    if ((c = getc(handle->file)) == EOF) {
  247.       return (0);
  248.       }
  249.  
  250.    *line_number = c;
  251.  
  252.    if ((c = getc(handle->file)) == EOF)
  253.       return (-1);
  254.  
  255.    *line_number += c*256;
  256.  
  257.    if (((line_data->red = (unsigned char *) malloc(handle->width))==NULL) ||
  258.        ((line_data->green = (unsigned char *) malloc(handle->width))==NULL) ||
  259.        ((line_data->blue = (unsigned char *) malloc(handle->width))==NULL)) {
  260.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", handle->filename);
  261.       close_all();
  262.       exit(1);
  263.       }
  264.  
  265.    for (i = 0 ; i < handle->width ; i++) {
  266.       line_data->red[i] = 0;
  267.       line_data->green[i] = 0;
  268.       line_data->blue[i] = 0;
  269.       }
  270.  
  271.    for (i = 0 ; i < handle->width ; i++) {
  272.       if ((data = getc(handle->file)) == EOF)
  273.          return(-1);
  274.  
  275.       line_data->red[i] = (unsigned char) data;
  276.       }
  277.  
  278.    for (i = 0 ; i < handle->width ; i++) {
  279.       if ((data = getc(handle->file)) == EOF)
  280.          return(-1);
  281.  
  282.       line_data->green[i] = (unsigned char) data;
  283.       }
  284.  
  285.    for (i = 0 ; i < handle->width ; i++) {
  286.       if ((data = getc(handle->file)) == EOF)
  287.          return(-1);
  288.  
  289.       line_data->blue[i] = (unsigned char) data;
  290.       }
  291.  
  292.    return (1);
  293.    }
  294.  
  295. void Close_Dump_File (handle)
  296.    FILE_HANDLE *handle;
  297.    {
  298.    fclose (handle->file);
  299.    if (handle->buffer_size != 0)
  300.       free (handle->buffer);
  301.    }
  302.  
  303. void Read_Dump_Image(Image, name)
  304.    IMAGE *Image;
  305.    char *name;
  306.    {
  307.    int rc, row, data1, data2;
  308.    struct Image_Line line;
  309.    FILE_HANDLE handle;
  310.  
  311.    if ((handle.file = Locate_File (name, "rb")) == NULL) {
  312.       fprintf (stderr, "Cannot open dump file %s\n", name);
  313.       close_all();
  314.       exit(1);
  315.       }
  316.  
  317.    if (((data1 = getc(handle.file)) == EOF)
  318.        || ((data2 = getc(handle.file)) == EOF)) {
  319.  
  320.       fprintf (stderr, "Cannot open dump file %s\n", name);
  321.       close_all();
  322.       exit(1);
  323.       }
  324.  
  325.    Image->iwidth  = data2 * 256 + data1;
  326.    handle.width = Image->iwidth;
  327.  
  328.    if (((data1 = getc(handle.file)) == EOF)
  329.        || ((data2 = getc(handle.file)) == EOF)) {
  330.  
  331.       fprintf (stderr, "Cannot open dump file %s\n", name);
  332.       close_all();
  333.       exit(1);
  334.       }
  335.  
  336.    Image->iheight = data2 * 256 + data1;
  337.    handle.height = Image->iheight;
  338.  
  339.    Image->width = (DBL)Image->iwidth;
  340.    Image->height = (DBL)Image->iheight;
  341.  
  342.    Image->Colour_Map_Size = 0;
  343.    Image->Colour_Map = NULL;
  344.  
  345.    if ((Image->data.rgb_lines = (struct Image_Line *)
  346.           malloc(Image->iheight * sizeof (struct Image_Line))) == NULL) {
  347.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", name);
  348.       exit(1);
  349.       }
  350.  
  351.    while ((rc = Read_Dump_Int_Line(&handle, &line, &row)) == 1)
  352.       Image->data.rgb_lines[row] = line;
  353.  
  354.    fclose (handle.file);
  355.  
  356.    if (rc == 0)
  357.       return;
  358.    else {
  359.       close_all();
  360.       exit(1);
  361.       }
  362.    }
  363.